/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.gui.view.internal.panel;

import com.genia.toolbox.basics.editor.gui.view.internal.panel.AbstractViewPanel;
import com.genia.toolbox.basics.editor.gui.view.internal.panel.ElementViewPanel;
import com.genia.toolbox.form.editor.gui.FormEditorGUI;
import com.genia.toolbox.form.editor.gui.view.internal.FormDocumentInternalFrame;
import com.genia.toolbox.form.editor.model.bean.FormInitialSettings;
import com.genia.toolbox.form.editor.model.form.impl.ContainerFormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormModel;
import com.genia.toolbox.form.editor.model.form.impl.FormType;

/**
 * The abstract view panel.
 */
@SuppressWarnings("serial")
public abstract class AbstractFormViewPanel
    extends AbstractViewPanel<FormEditorGUI, FormDocumentInternalFrame, FormModel, FormType>
{

  /**
   * Constructor of a root panel.
   * 
   * @param gui
   *          the gui.
   * @param internalFrame
   *          the internal frame.
   */
  public AbstractFormViewPanel(FormEditorGUI gui, FormDocumentInternalFrame internalFrame)
  {
    super(gui, internalFrame);
  }



  /**
   * Constructor of a sub-panel.
   * 
   * @param internalFrame
   *          the internal frame.
   * @param parentPanel
   *          the parent panel.
   */
  public AbstractFormViewPanel(FormDocumentInternalFrame internalFrame, AbstractContainerFormViewPanel parentPanel)
  {
    super(internalFrame, parentPanel);
  }



  /**
   * Add a sub-element.
   */
  @Override
  public void addSubElement()
  {
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    FormInitialSettings settings = this.getGui().createSubElement();
    this.getGui().getController().notifyCreateSubElement(settings, containerElementModel.getDocumentID(), containerElementModel.getUniqueID(), containerElementModel.getDirection());
  }



  /**
   * Add a sub-form.
   */
  public void addSubForm()
  {
    ContainerFormModel containerElementModel = (ContainerFormModel) this.getElement();
    FormInitialSettings settings = this.getGui().createSubForm();
    this.getGui().getController().notifyCreateSubElement(settings, containerElementModel.getDocumentID(), containerElementModel.getUniqueID(), containerElementModel.getDirection());
  }



  /**
   * Delete a sub-element.
   */
  @Override
  public void deleteSubElement()
  {
    this.getInternalFrame().notifyDocumentChanges();
    this.getGui().displayElementSettings(null);
    AbstractContainerFormViewPanel containerPanel = (AbstractContainerFormViewPanel) this.getParentPanel();
    containerPanel.deactivateAll();
    containerPanel.removeSubElement(this.getElement());
    this.updateBorder();
    ElementViewPanel<FormEditorGUI, FormDocumentInternalFrame, FormModel, FormType> panel = this;
    while (panel != null) {
      panel.updateBorder();
      panel = panel.getParentPanel();
    }
  }



  /**
   * Move down or left a sub-element.
   */
  @Override
  public void moveDownOrLeft()
  {
    this.getInternalFrame().notifyDocumentChanges();
    AbstractContainerFormViewPanel containerPanel = (AbstractContainerFormViewPanel) this.getParentPanel();
    containerPanel.moveDownSubElement(this.getElement());
  }



  /**
   * Move up or right a sub-element.
   */
  @Override
  public void moveUpOrRight()
  {
    this.getInternalFrame().notifyDocumentChanges();
    AbstractContainerFormViewPanel containerPanel = (AbstractContainerFormViewPanel) this.getParentPanel();
    containerPanel.moveUpSubElement(this.getElement());
  }



  /**
   * Get the id label text.
   * 
   * @param model
   *          the model.
   * @return the id label text.
   */
  @Override
  public String getLabelIdText(FormModel model)
  {
    return "";
  }

}
